package com.xiaoyun.utils;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

/**
 * 通用脱敏工具类
 * 可用于：
 * 用户名
 * 手机号
 * 邮箱
 * 地址等
 */
public class DesensitizationUtil {

    private static final int SIZE = 6;
    private static final String SYMBOL = "*";


    public static void main(String[] args) {

        String name = commonDisplay("muxin");
        String mobile = commonDisplay("13900000000");
        String mail = commonDisplay("wgy952046097@gmail.com");
        String address = commonDisplay("北京大运河东路888号");
        List<UserTest> users = Arrays.asList(
                new UserTest("name1", "mobile", "mail", "address"),
                new UserTest("name2", "mobile", "mail", "address"),
                new UserTest("name3", "mobile", "mail", "address"),
                new UserTest("name4", "mobile", "mail", "address")
        );
        System.out.println(users);
        users.forEach(item->{
            item.setName(commonDisplay(item.getName()));
        });
        System.out.println(users);

//        System.out.println(name);
//        System.out.println(mobile);
//        System.out.println(mail);
//        System.out.println(address);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static
    class UserTest {
        String name;
        String mobile;
        String mail;
        String address;
    }

    /**
     * 通用脱敏方法
     *
     * @param value
     * @return
     */
    public static String commonDisplay(String value) {
        if (null == value || "".equals(value)) {
            return value;
        }
        int len = value.length();
        int pamaone = len / 2;
        int pamatwo = pamaone - 1;
        int pamathree = len % 2;
        StringBuilder stringBuilder = new StringBuilder();
        if (len <= 2) {
            if (pamathree == 1) {
                return SYMBOL;
            }
            stringBuilder.append(SYMBOL);
            stringBuilder.append(value.charAt(len - 1));
        } else {
            if (pamatwo <= 0) {
                stringBuilder.append(value.substring(0, 1));
                stringBuilder.append(SYMBOL);
                stringBuilder.append(value.substring(len - 1, len));

            } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
                int pamafive = (len - SIZE) / 2;
                stringBuilder.append(value.substring(0, pamafive));
                for (int i = 0; i < SIZE; i++) {
                    stringBuilder.append(SYMBOL);
                }
                if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
                    stringBuilder.append(value.substring(len - pamafive, len));
                } else {
                    stringBuilder.append(value.substring(len - (pamafive + 1), len));
                }
            } else {
                int pamafour = len - 2;
                stringBuilder.append(value.substring(0, 1));
                for (int i = 0; i < pamafour; i++) {
                    stringBuilder.append(SYMBOL);
                }
                stringBuilder.append(value.substring(len - 1, len));
            }
        }
        return stringBuilder.toString();
    }

}
